/*
Author: Drew Risinger, Chris Cochoff, Jordan Walsh, Cameron Holloway
Course: COMP 220, Computer Programming II
Date: February 24, 2014
Description: This is the .cpp implementation for the Game class header. It contains the rules for the
			 game, as well as the general control for the different user inputs and situations relating
			 to blackjack.
*/
#include "Game.h"
#include <iostream>
#include <string>
#include <vector>

Game::Game(){
	createPlayer();
	over=false;
	displayRules();
	playHand();
}

//Displays Rules and quick instructions for playing
void Game::displayRules(){
	cout<<endl;
	cout << "Here are the rules..." << endl;
	cout<<endl;
	cout<<"1. The objective of the game is to get 21, or as close to it as possible."<<endl;
	cout<<"2. You will be dealt 2 cards, and then you can either 'hit' or 'stand'"<<endl;
	cout<<"3. Hitting gives you another card."<<endl;
	cout<<"4. Standing ends your turn, and then the dealer goes."<<endl;
	cout<<"5. The dealer hits until they get at least 17."<<endl;
	cout<<"6. If you go over 21, you 'bust', meaning your turn ends and you lose."<<endl;
	cout<<"7. You win if you get higher than the dealer, and lose otherwise."<<endl;
	cout<<"8. If you are dealt a card value of 21 to begin with, you get blackjack!"<<endl;
	cout<<endl;
	cout<<"Press D to receive your hand...";
	char response;
	cin>>response;
	response = toupper(response);
	if(response == 'D') {
		system("cls");
		return;
	}
	else {
		while(response != 'D') {
			cout<<"Please hit D to continue...";
			cin>>response;
			response = toupper(response);
			if(response == 'D') {
				system("cls");
				break;
			}
			else
				continue;
		}
	}
}

//prompts user for input to create their player, then adds it to list of players
void Game::createPlayer(){
	string name;
	cout << "Enter name" << endl;
	cin >> name;
	Player human(name, Human);
	players.push_back(human);
	Player dealer("dealer", Dealer);
	players.push_back(dealer);
}

//All players' actions for one single hand/deal of the game. 
void Game::playHand(){

	//Initial Deal
	unsigned int initialDraws=2;
	for(unsigned int i=0; i < players.size(); i++){
		for(unsigned int j=0; j < initialDraws; j++){
			players.at(i).drawCard(mainDeck);
		}
	}


	//show dealer top card
	players.at(1).displayDealerHand();

	//players.at(0) is the human player
	//players.at(1) is the dealer
	players.at(0).chooseHumanAction(mainDeck);
	if(!players.at(0).isBusted())
		players.at(1).chooseComputerAction(mainDeck);
	if(!players.at(0).isBusted() && (players.at(0).handValue() > players.at(1).handValue()  || players.at(1).isBusted())){
		cout << "Congrats, " << players.at(0).getName() << ", you won!" << endl;
		cout<<"Press Y to play again, or any other key to exit...";
		char response;
		cin>>response;
		response = toupper(response);
		if(response == 'Y') {
			for(int k=0; k<players.size(); k++){
				players.at(k).clearHand();
			}
				playHand();
		}
		else {
			cout<<"Thanks for playing!"<<endl;
			over = true;
		}
	}
	else if((players.at(1).handValue() >= players.at(0).handValue() && !players.at(1).isBusted()) || players.at(0).isBusted()){
		cout << "Sorry, " << players.at(0).getName() << ", you lost" << endl;

		cout<<"Press Y to play again, or any other key to exit...";
		char response;
		cin>>response;
		response = toupper(response);
		if(response == 'Y') {
			for(int k=0; k<players.size(); k++){
				players.at(k).clearHand();
			}

			playHand();
		}
		else {
			cout<<"Thanks for playing!"<<endl;
			over = true;
		}
	}
}

//getter method to check if the game has finished
bool Game::isOver(){
	return over;
}

void Game::endGame(){
	cout << "It's Over!" << endl;
}

